package org.dfl.plugin.views;

import java.util.HashSet;
import java.util.Set;

import org.dfl.plugin.editor.DflEditor;
import org.dfl.plugin.views.debug.HistoriesTreeView;
import org.dfl.plugin.views.debug.TokensTreeView;
import org.dfl.plugin.views.debug.TreeElemPreview;
import org.dfl.plugin.views.debug.model.MapKeyTreeElem;
import org.dfl.plugin.views.debug.model.TreeElem;
import org.eclipse.jface.viewers.SelectionChangedEvent;

/**
 * A views manager to coordinate listeners between views and editors.
 * Each editor and all used views (HistoriesTreeView, TokensTreeView, TreeElemPreview)
 * register in this class to register listener properly.
 * @author Piotr Wlodarczyk
 *
 */
public class ViewsManager {
	private static Set<DflEditor> editors = new HashSet<DflEditor>();
	private static HistoriesTreeView historiesTreeView;
	private static TokensTreeView tokensTreeView;
	private static TreeElemPreview treeElemPreview;
	
	public static void registerHistoriesTreeView(HistoriesTreeView historiesTreeView){
		ViewsManager.historiesTreeView = historiesTreeView;
		for(DflEditor editor : editors){
			editor.getViewer().addSelectionChangedListener(historiesTreeView);
			historiesTreeView.selectionChanged(new SelectionChangedEvent(editor.getViewer(),editor.getViewer().getSelection()));
		}
		if(tokensTreeView!=null){
			historiesTreeView.getCommonViewer().addSelectionChangedListener(tokensTreeView);
			tokensTreeView.selectionChanged(new SelectionChangedEvent(historiesTreeView.getCommonViewer(),historiesTreeView.getCommonViewer().getSelection()));
		}
		if(treeElemPreview!=null){
			historiesTreeView.getCommonViewer().addSelectionChangedListener(treeElemPreview);
			treeElemPreview.selectionChanged(new SelectionChangedEvent(historiesTreeView.getCommonViewer(),historiesTreeView.getCommonViewer().getSelection()));
		}
	}
	
	public static void registerTokensTreeView(TokensTreeView tokensTreeView){
		ViewsManager.tokensTreeView = tokensTreeView;
		if(historiesTreeView!=null){
			historiesTreeView.getCommonViewer().addSelectionChangedListener(tokensTreeView);
			tokensTreeView.selectionChanged(new SelectionChangedEvent(historiesTreeView.getCommonViewer(),historiesTreeView.getCommonViewer().getSelection()));
		}
		if(treeElemPreview!=null){
			tokensTreeView.getCommonViewer().addSelectionChangedListener(treeElemPreview);
			treeElemPreview.selectionChanged(new SelectionChangedEvent(tokensTreeView.getCommonViewer(),tokensTreeView.getCommonViewer().getSelection()));
		}
	}
	
	public static void registerEditor(DflEditor editor){
		editors.add(editor);
		if(historiesTreeView!=null){
			editor.getViewer().addSelectionChangedListener(historiesTreeView);
			historiesTreeView.selectionChanged(new SelectionChangedEvent(editor.getViewer(),editor.getViewer().getSelection()));
		}
	}
	
	public static void registerViewer(TreeElemPreview treeElemPreview2){
		ViewsManager.treeElemPreview = treeElemPreview2;
		if(historiesTreeView!=null){
			historiesTreeView.getCommonViewer().addSelectionChangedListener(treeElemPreview2);
		}
		
		if(tokensTreeView!=null){
			tokensTreeView.getCommonViewer().addSelectionChangedListener(treeElemPreview2);
		}
	}
	
	public static void refreshViews(Object elem){
		// to refresh a child of a map elem
		if(elem instanceof TreeElem && ((TreeElem)elem).getParent() instanceof MapKeyTreeElem){
			elem = ((TreeElem)elem).getParent();
		}
		HistoriesTreeView historiesTreeView = HistoriesTreeView.getView();
		if(historiesTreeView!=null){
			historiesTreeView.getCommonViewer().refresh(elem);
		}
		
		TokensTreeView tokensTreeView = TokensTreeView.getView();
		if(tokensTreeView!=null){
			tokensTreeView.getCommonViewer().refresh(elem);
		}
	}
	
	public static void refreshViews(){
		HistoriesTreeView historiesTreeView = HistoriesTreeView.getView();
		if(historiesTreeView!=null){
			historiesTreeView.refresh();
		}
		
		TokensTreeView tokensTreeView = TokensTreeView.getView();
		if(tokensTreeView!=null){
			tokensTreeView.refresh();
		}
	}
}
